package n.info.adapters;

import n.info.R;
import n.info.entities.FeedItem;
import n.info.media.ImageManager;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FeedItemAdapter extends BaseAdapter {

	// ~ constants .................................................................................
	// ~ Static fields/initializers .............................................................
	// ~ Static methods ........................................................................
	// ~ Instance fields .........................................................................
	
	FeedItem[] array ; 
	LayoutInflater inflater ;
	
	// ~ Constructors ...........................................................................
	
	public FeedItemAdapter(Context context, FeedItem[] array){
		this.array = array ;
		inflater = LayoutInflater.from(context);
	}
	
	// ~ Overriden methods .................................................................
	
	@Override
	public int getCount() {
		return array.length ;
	}

	@Override
	public Object getItem(int index) {
		return array[index];
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		convertView = inflater.inflate(R.layout.feed_reader_item_pic, null);
		
		TextView title = (TextView) convertView.findViewById(R.id.textViewFeedTitle);
		TextView desc = (TextView) convertView.findViewById(R.id.textViewFeedDescription);
		ImageView img = (ImageView) convertView.findViewById(R.id.imageViewFeedItem);
		
		FeedItem feedItem = (FeedItem) getItem(position);
		title.setText(feedItem.getTitle());
		desc.setText(Html.fromHtml(feedItem.getDescription()).toString());
		
		String url = null ;
		if(feedItem.getEnclosure() != null ){
			url = feedItem.getEnclosure().getUrl() ;
		}

		ImageManager.getDefault().loadBitmapOnThread(url, img);
		return convertView;
	}

	// ~ Methods ..................................................................................	
	// ~ Inner classes ...........................................................................
	
}
